<?php

require_once("function.php");
require_once("User.php");

class accountController
{
	function isValidData($TIN, $employeeNo, $firstName, $middleName, $lastName, $contactNo, $birthday, $position, $branch, $branchAdd, $password)
	{
		if (strlen($TIN) < 12 && !is_numeric($TIN))
		{
			return false;	
		}
		else if (strlen($employeeNo) < 10 || empty($firstName) || empty($middleName) || empty($lastName) || empty($position) || empty($branch) )
		{
			return false;
		}
		else if (empty($branchAdd) || strlen($password) < 6 || empty($birthday))
		{
			return false;
		}
		return true;
	}
	
	function sendAccount($TIN, $employeeNo, $firstName, $middleName, $lastName, $contactNo, $birthday, $position, $branch, $branchAdd, $password)
	{
		if(self::isValidData($TIN, $employeeNo, $firstName, $middleName, $lastName, $contactNo, $birthday, $position, $branch, $branchAdd, $password))
		{
			$user = new User;
			$user->create($TIN, $employeeNo, $firstName, $middleName, $lastName, $contactNo, $birthday, $position, $branch, $branchAdd, $password);
			$_SESSION['stat'] = "create_success";
			$_SESSION['msg'] = "Registration successful! You may now login using your account";
			header("Location: index.php");
		}
		else
		{
			$_SESSION['stat'] = "create_error";
			$_SESSION['msg'] = "You have entered invalid data, please fill up the form and make sure that the information you enter are correct";
			header("Location: createAccountView.php");
		}
			
	}
}

$create = new accountController;
$TIN = $_REQUEST['tin'];
$employeeNo = $_REQUEST['employeeNumber'];
$firstName = $_REQUEST['firstName'];
$middleName = $_REQUEST['middleName'];
$lastName = $_REQUEST['lastName'];
$contactNo = $_REQUEST['contactNumber'];
$birthday = $_REQUEST['birthday'];
$position = $_REQUEST['position'];
$branch = $_REQUEST['branch'];
$branchAdd = $_REQUEST['branchAddress'];
$password = $_REQUEST['password'];

echo $TIN;

$create->sendAccount($TIN, $employeeNo, $firstName, $middleName, $lastName, $contactNo, $birthday, $position, $branch, $branchAdd, $password);
?>